/*
 * dessin.c
 *
 *  Created on: 17 sept. 2013
 *      Author: the
 */
#include "dessin.h"

/* Dessine le graphe des noeuds					*/
void dessineGraph()
{
	int i, j, k, tmp, taille, id;
	double radius;
	FILE *GRAPH;

	GRAPH = fopen("graph.eps","w+");

	taille=(int) (ceil(SCALE*1.0*L)+1.0*SCALE+200.0);
	fprintf(GRAPH,"%%!PS-Adobe-3.0\n");
	fprintf(GRAPH,"%%%%BoundingBox:  %d %d %d %d\n",0,0,taille,taille);

	radius=5.0;


	for(i=0;i<nbOfNodes;i++){
		fprintf(GRAPH,"gsave 0.0 0.0 0.0 setrgbcolor\n");
		fprintf(GRAPH,"%d %d %d 0 360 arc\n",100+(int) floor(SCALE*nodes[i].x),100+(int) floor(SCALE*nodes[i].y),(int) floor(SCALE*radius));
		fprintf(GRAPH,"gsave 1.0 1.0 0.0 setrgbcolor fill grestore 0.1 setlinewidth\n");
		fprintf(GRAPH,"stroke\n");

		//Set the font and police
		fprintf(GRAPH,"/Times-Roman findfont\n");
		fprintf(GRAPH,"12 scalefont\n");
		fprintf(GRAPH,"setfont\n");
		fprintf(GRAPH,"newpath\n");
     		fprintf(GRAPH,"%d %d moveto\n",100+(int) floor(SCALE*nodes[i].x+SCALE*radius)+5,100+(int) floor(SCALE*nodes[i].y-SCALE*radius)+5);
    		fprintf(GRAPH,"(%d) show\n",i);
    		fprintf(GRAPH,"closepath\nstroke\n");

		for(j=0;j<nodes[i].nbN1;j++){
			tmp=2;
			id=nodes[i].N1[j].id;//index du jeme voisin
			//On ne trace que les aretes des voisins ayant un index plus grand (cela evite de les tracer deux fois)
			if(i<id){
				//frequence 0
				//fprintf(GRAPH,"gsave 0.0 0.0 0.0 setrgbcolor\n");
				//fprintf(GRAPH,"newpath\n");
     				//fprintf(GRAPH,"%d %d moveto\n",100+(int) floor(SCALE*nodes[i].x+SCALE*radius)-5,100+(int) floor(SCALE*nodes[i].y-SCALE*radius)+5);
     				//fprintf(GRAPH,"%d %d lineto\n",100+(int) floor(SCALE*nodes[id].x+SCALE*radius)-5,100+(int) floor(SCALE*nodes[id].y-SCALE*radius)+5);
				//fprintf(GRAPH,"closepath\n stroke\n");


				//Les autres frequences
				for(k=0;k<maxFreq;k++){
					if(linkFrequency(i,id,k)>0){
						if(k==0) fprintf(GRAPH,"gsave 0.0 0.0 0.0 setrgbcolor\n");
						if(k==1) fprintf(GRAPH,"gsave 1.0 0.0 0.0 setrgbcolor\n");
						if(k==2) fprintf(GRAPH,"gsave 0.0 1.0 0.0 setrgbcolor\n");
						if(k==3) fprintf(GRAPH,"gsave 0.0 0.0 0.5 setrgbcolor\n");
						if(k==4) fprintf(GRAPH,"gsave 1.0 0.7 0.0 setrgbcolor\n");
						if(k==5) fprintf(GRAPH,"gsave 1.0 0.0 1.0 setrgbcolor\n");
						if(k==6) fprintf(GRAPH,"gsave 0.0 1.0 1.0 setrgbcolor\n");
						if(k==7) fprintf(GRAPH,"gsave 0.6 0.4 0.1 setrgbcolor\n");

						fprintf(GRAPH,"newpath\n");
     						fprintf(GRAPH,"%d %d moveto\n",100+(int) floor(SCALE*nodes[i].x+SCALE*radius)-5+tmp,100+(int) floor(SCALE*nodes[i].y-SCALE*radius)+5-tmp);
     						fprintf(GRAPH,"%d %d lineto\n",100+(int) floor(SCALE*nodes[id].x+SCALE*radius)-5+tmp,100+(int) floor(SCALE*nodes[id].y-SCALE*radius)+5-tmp);
						//color=1.0*k/(1.0*maxFreq);
						//fprintf(GRAPH,"gsave 1.0 %f %f setrgbcolor\n",color,color);
						fprintf(GRAPH,"closepath\n stroke\n");
						tmp+=2;//tmp permet de decaler les nouvelles arretes
					}
				}

			}

		}
	}


	fclose(GRAPH);

}//end of function dessineGraph




